package com.graduation.vaccine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.vaccine.exception.GlobalException;
import com.graduation.vaccine.mapper.UserVaccinedMapper;
import com.graduation.vaccine.mapper.VaccinatedInfoMapper;
import com.graduation.vaccine.pojo.VaccinatedInfo;
import com.graduation.vaccine.service.VaccinatedInfoService;
import com.graduation.vaccine.vo.AddFamilyVo;
import com.graduation.vaccine.vo.RespBean;
import com.graduation.vaccine.vo.RespBeanEnum;
import com.graduation.vaccine.vo.UpdateVaccinatednVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoJunYao
 * @since 2022-04-22
 */
@Service
public class VaccinatedInfoServiceImpl extends ServiceImpl<VaccinatedInfoMapper, VaccinatedInfo> implements VaccinatedInfoService {

   @Autowired
   private VaccinatedInfoMapper vaccinatedInfoMapper;
   @Autowired
   private UserVaccinedMapper userVaccinedMapper;

    /**
     * 根据手机号获取家人列表
     * @param phone
     * @return
     */
    @Override
    public RespBean getFamily(String phone) {
        return RespBean.success(vaccinatedInfoMapper.getFamily(phone));
    }

    /**
     * 添加家人
     * @param addFamilyVo
     * @return
     */
    @Override
    public RespBean addFamily(AddFamilyVo addFamilyVo) {

        String IDCard=addFamilyVo.getFamilyDTO().getIdCardNumber();//获取用户的身份证号，判断是否注册过
        List<VaccinatedInfo> vaccinatedInfos=vaccinatedInfoMapper.getListByIDCard(IDCard);
        if(vaccinatedInfos!=null && !vaccinatedInfos.isEmpty()){
            return RespBean.error(RespBeanEnum.DUPLICATEKEY_IDCard_ERROR);
        }

        String phone=addFamilyVo.getPhone();
//        int row=vaccinatedInfoMapper.addFamily(addFamilyVo.getFamilyDTO());

        try {
            if(vaccinatedInfoMapper.addFamily(addFamilyVo.getFamilyDTO())<=0 || userVaccinedMapper.addOne(phone,IDCard)<=0){
                return RespBean.error(RespBeanEnum.UPDATE_ERROR);
            }
        }catch (Exception e){
            throw new GlobalException(RespBeanEnum.UPDATE_ERROR);
        }

        return RespBean.success("添加成功");
    }

    @Override
    public RespBean updateVaccinatedInfo(UpdateVaccinatednVo updateVaccinatednVo) {
        if(vaccinatedInfoMapper.updateVaccinatedInfo(updateVaccinatednVo)<=0){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR);
        }
        return RespBean.success(RespBeanEnum.SUCCESS);
    }


}
